package com.qianfeng.wuyang.himalaya.adapter.recommendadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.wuyang.himalaya.R;
import com.qianfeng.wuyang.himalaya.bean.recommendbean.ListenInfoEntity;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by WangQing on 2016/12/9.
 */

public class LisstenRecommendApadter extends RecyclerView.Adapter<LisstenRecommendApadter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<ListenInfoEntity> infoList;

    public LisstenRecommendApadter(Context context,List<ListenInfoEntity> infoList) {
        this.context = context;
        this.infoList = infoList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.author.setText(infoList.get(position).getTitle());
        holder.title.setText(infoList.get(position).getTrackTitle());
        if (infoList.get(position).getCoverLarge()!=null) {
            Picasso.with(context).load(infoList.get(position).getCoverLarge()).into(holder.img);

        }
        holder.layout.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {

        return infoList.size();
    }

    @Override
    public void onClick(View view) {
        int position=((RecyclerView)view.getParent()).getChildAdapterPosition(view);
        Toast.makeText(context,"点击第"+position+"个",Toast.LENGTH_SHORT).show();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private  ImageView img;
        private  TextView title;
        private  TextView author;
        private LinearLayout layout;
        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.recommend_item_img);
            title = (TextView) itemView.findViewById(R.id.recommend_item_title);
            author = (TextView) itemView.findViewById(R.id.recommend_item_author);
            layout = (LinearLayout) itemView.findViewById(R.id.recommend_item_layout);
        }
    }
}
